package mdm.led.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import mdm.led.dao.DepartmentMapper;
import mdm.led.dao.DopeMapper;
import mdm.led.dao.UserMapper;
import mdm.led.entity.AdminGetDope;
import mdm.led.entity.Dope;
import mdm.led.entity.UnreadReadEstate;
import mdm.led.entity.User;
import mdm.led.entity.department.Department;
import mdm.led.service.DopeService;

/**
 *
 * @author :Monarch丿He
 * @date :2018年11月2日
 * @explain : 消息 服务 接口实现
 */
@Service("dopeServiceImpl")
public class DopeServiceImpl implements DopeService {

	@Autowired
	private DopeMapper dopeMapper;

	@Autowired
	private UserMapper userDao;

	@Autowired
	private DepartmentMapper departmentDao;

	@Override
	public int addAppointManDope(Dope dope) {
		// 当前时间
		Date date = new Date();
		dope.setTime(date);
		dope.setType(4);
		dope.setVisual(1);
		dope.setStatus(1);
		int iRet = dopeMapper.addAppointManDope(dope);
		// 接收消息的人
		String receiveMan = dope.getReceiveMan();
		// 接收消息部门
		String receiveBranch = dope.getReceiveBranch();
		dope.setStatus(2);
		dope.setReceiveBranch(null);
		List<String> manStr = new ArrayList<>();
		if (receiveMan != null && !(receiveMan.equals(""))) {
			String[] mans = receiveMan.split(",");
			for (String item : mans) {
				dope.setReceiveMan(item);
				dopeMapper.addAppointManDope(dope);
				manStr.add(item);
			}
		}
		if (receiveBranch != null && !(receiveBranch.equals(""))) {
			// 部门id集合
			String[] branch = receiveBranch.split(",");
			// 所有用户
			List<User> users = userDao.findAllDoUser();
			// 需要发送的用户集合
			List<String> dopeUser = new ArrayList<>();

			for (String i : branch) {
				for (User it : users) {
					if (Integer.parseInt(i) == it.getDepartmentId()) {
						dopeUser.add(String.valueOf(it.getcUserId()));
					}
				}
			}

			if (dopeUser.size() > 0) {
				for (String item : dopeUser) {
					dope.setReceiveMan(item);
					dopeMapper.addAppointManDope(dope);
					manStr.add(item);
				}
			}
		}

		return iRet;
	}

	@Override
	public int updateNoVisual(Integer id) {
		return dopeMapper.updateNoVisual(id);
	}

	@Override
	public List<Dope> findAllDopes(Integer userid, Integer type) {
		// 取出所有消息集合
		List<Dope> list = dopeMapper.findDopeByTypeAndUid(type, String.valueOf(userid));

		for (Dope item : list) {
			if (Integer.parseInt(item.getReceiveMan()) == userid) {
				dopeMapper.updateNoVisual(item.getDopeId());
			}
		}
		return list;
	}

	@Override
	public List<User> findAllDoUser() {
		return userDao.findAllDoUser();
	}

	@Override
	public List<Department> findAllDepartment() {
		// 临时部门集合
		List<Department> list = new ArrayList<>();
		// 实例化一个部门实体
		Department department = new Department();
		// 类型为 科室
		department.setType(2);
		// 查询所有科室
		List<Department> findDepartment = departmentDao.findDepartment(department);
		// 添加到 部门集合
		for (Department item : findDepartment) {
			list.add(item);
		}
		// 类型为 大队
		department.setType(3);
		// 查询所有大队
		findDepartment = departmentDao.findDepartment(department);
		// 添加到 部门集合
		for (Department item : findDepartment) {
			list.add(item);
		}

		return list;
	}

	@Override
	public UnreadReadEstate findUnreadReadEstate(Integer userid) {
		// 取出所有消息集合
		List<Dope> list = dopeMapper.findDopeByType(0);
		// 取出符合要求消息返回
		List<Dope> userDopes = new ArrayList<Dope>();
		String receiveMan = "";
		// String receiveBranch = "";
		// 接收对象
		String[] users = null;
		// 接收部门
		// String[] branch = null;
		for (Dope item : list) {
			// boolean iRet = false;
			if (item.getVisual() == 2) {
				continue;
			}
			if ("".equals(item.getReceiveBranch())) {
				continue;
			}
			// 接收对象拼接字符
			receiveMan = item.getReceiveMan();
			if (receiveMan != null && !(receiveMan.equals(""))) {
				// 分割用户为数组
				users = receiveMan.split(",");
				for (String i : users) {
					if (Integer.parseInt(i) == userid) {
						userDopes.add(item);
						// iRet = true;
						break;
					}
				}
			}

		}
		// // 消息状态数量实体
		UnreadReadEstate unread = new UnreadReadEstate();
		// 督导三查
		int three = 0;
		// 督查四必
		int four = 0;
		// 流程管控
		int five = 0;
		// 系统通知
		int system = 0;
		// 总数
		int count = userDopes.size();

		for (Dope item : userDopes) {
			switch (item.getType()) {
			case 1:
				three++;
				break;
			case 2:
				four++;
				break;
			case 3:
				five++;
				break;
			case 4:
				system++;
				break;
			}

		}
		unread.setThree(three);
		unread.setFour(four);
		unread.setFive(five);
		unread.setSystem(system);
		unread.setCount(count);

		return unread;
	}

	@Override
	public List<Dope> findUserPopDopes(Integer userid) {

		// 取出所有消息集合
		List<Dope> list = dopeMapper.findDope();
		// 取出符合要求消息返回
		List<Dope> userDopes = new ArrayList<Dope>();
		String receiveMan = "";
		String receiveBranch = "";
		// 接收对象
		String[] users = null;
		// 接收部门
		String[] branch = null;
		for (Dope item : list) {
			boolean iRet = false;
			if (item.getIfPop() == 2) {
				continue;
			}
			// 接收对象拼接字符
			receiveMan = item.getReceiveMan();
			// 分割用户为数组
			users = receiveMan.split(",");
			for (String i : users) {
				if (Integer.parseInt(i) == userid) {
					userDopes.add(item);
					iRet = true;
					break;
				}
			}
			// 接收部门拼接字符
			receiveBranch = item.getReceiveBranch();
			if (!iRet && receiveBranch != null && !(receiveBranch.equals(""))) {
				// 分割部门 为 数组
				branch = receiveBranch.split(",");
				int exisUser = 0;
				for (String i : branch) {
					exisUser = dopeMapper.findDepartmentExistUser(Integer.parseInt(i), userid);
					if (exisUser > 0) {
						userDopes.add(item);
						iRet = true;
						break;
					}
				}
			} else {
				continue;
			}

		}

		return userDopes;
	}

	@Override
	public List<AdminGetDope> findHistoryDope() {
		// 取出所有消息集合
		List<Dope> list = dopeMapper.findDopeByType(4);
		// 取出符合要求消息返回
		List<AdminGetDope> adminGetDope = new ArrayList<AdminGetDope>();
		String receiveMan = "";
		String receiveBranch = "";
		AdminGetDope getDopes = null;
		// 接收对象
		String[] users = null;
		// 接收部门
		String[] branch = null;
		List<String> receiveMans = null;
		List<String> receiveBranchs = null;
		User user = null;
		Department department = null;

		// 时间格式化
		SimpleDateFormat si = new SimpleDateFormat("yyyy-MM-dd  HH:mm");
		for (Dope item : list) {
			if (item.getStatus() == 2) {
				continue;
			}
			// set 属性
			getDopes = new AdminGetDope();
			getDopes.setTitle(item.getTitle());
			getDopes.setContent(item.getContent());
			getDopes.setTime(si.format(item.getTime()));
			getDopes.setIfPop(String.valueOf(item.getIfPop()));

			receiveMans = new ArrayList<>();
			receiveBranchs = new ArrayList<>();
			// 接收对象拼接字符
			receiveMan = item.getReceiveMan();
			if (receiveMan.length() > 0) {
				// 分割用户为数组
				users = receiveMan.split(",");
				for (String u : users) {
					user = userDao.selectById(Integer.parseInt(u));
					if (user != null) {
						receiveMans.add(user.getcNickname());
					}
				}
			}
			// 部门
			receiveBranch = item.getReceiveBranch();
			if (receiveBranch != null && !(receiveBranch.equals(""))) {
				// 分割用户为数组
				branch = receiveBranch.split(",");
				for (String d : branch) {
					department = departmentDao.findDepartmentById(Integer.parseInt(d));
					if (department != null) {
						receiveBranchs.add(department.getName());
					}
				}
			}
			// set 返回的 填写人 填写部门
			getDopes.setReceiveMan(receiveMans);
			getDopes.setReceiveBranch(receiveBranchs);

			adminGetDope.add(getDopes);

		}

		return adminGetDope;
	}

}
